package com.dgut.doctorservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dgut.commonutils.Result;
import com.dgut.doctorservice.entity.PatientInfo;
import com.dgut.doctorservice.entity.Reception;
import com.dgut.doctorservice.entity.vo.ReceptionQuery;
import com.dgut.doctorservice.service.ReceptionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dgut
 * @since 2021-09-28
 */
@RestController
@RequestMapping("/doctorservice/reception")
@CrossOrigin
public class ReceptionController {

    @Autowired
    ReceptionService receptionService;

    @ApiOperation("前台添加")
    @PostMapping("addReception")
    public Result addReception(@RequestBody Reception reception){
        receptionService.save(reception);
        return Result.succ();
    }

    @ApiOperation("修改前台登录密码")
    @PostMapping("updatePassword")
    public Result updatePassword(@RequestParam String id,@RequestParam String old,@RequestParam String update){
        Reception byId = receptionService.getById(id);
        if (byId.getPassword().equals(old)){
            byId.setPassword(update);
            receptionService.updateById(byId);
            return Result.succ();
        }else {
            return Result.succ().message("原密码错误！");
        }
    }

    @ApiOperation("前台登录")
    @PostMapping("loginReception")
    public Result loginReception(@RequestParam String phone,@RequestParam String password){
        QueryWrapper<Reception> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        Reception one = receptionService.getOne(wrapper);
        if (one==null){
            return Result.succ().message("账户不存在");
        }else {
            if (one.getPassword().equals(password)){
                return Result.succ().data("reception",one);
            }else {
                return Result.succ().message("密码错误！");
            }
        }
    }


    @ApiOperation("根据前台id修改个人信息，管理员与前台均可用")
    @PostMapping("updateReception")
    public Result updateReception(@RequestBody Reception reception){
        receptionService.updateById(reception);
        return Result.succ();
    }

    @ApiOperation("根据前台id删除")
    @DeleteMapping("deleteReceptionById")
    public Result deleteReceptionById(@RequestParam String id){
        receptionService.removeById(id);
        return Result.succ();
    }

    @ApiOperation("获取所有前台信息")
    @GetMapping("getAllReception")
    public Result getAllReception(){
        List<Reception> list = receptionService.list();
        return Result.succ().data("list",list);
    }

    @ApiOperation("条件组合查询前台信息")
    @PostMapping("searchReception")
    public Result searchReception(@RequestBody ReceptionQuery receptionQuery){
        String name = receptionQuery.getName();
        String idCard = receptionQuery.getIdCard();
        String phone = receptionQuery.getPhone();
        QueryWrapper<Reception> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(idCard)){
            wrapper.like("id_card",idCard);
        }
        if (!StringUtils.isEmpty(phone)){
            wrapper.like("phone",phone);
        }
        List<Reception> list = receptionService.list(wrapper);
        return Result.succ().data("list",list);
    }





}

